package servlet;

import domain.PageBean;
import domain.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/findPageUserServlet")
public class FindPageUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //1.获取参数
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");
        //这个判断是必要添加的,你直接访问,没有参数,或者你重定向到此资源.  会报错 空指针异常因为你没参数
        //反正你浏览器没给这两个参数,你就设为默认
        if (currentPage==null||"".equals(currentPage)){
            currentPage = "1";
        }
        if (rows==null||"".equals(rows)){
            rows = "5";
        }
        UserService service = new UserServiceImpl();
        //需要返回一个PageBean对象
        PageBean<User> pb = service.findPageUser(currentPage,rows);
        System.out.println(pb );

        request.setAttribute("pb",pb);
        request.getRequestDispatcher("/list.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request,response);
    }
}
